/*
 * HtmlComparisonDataTable.java
 * 
 * Created on 2007.06.20, 22:59:09
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hr.web.jsf.components;

import java.io.IOException;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.html.HtmlForm;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

/**
 * 
 * @author Administrator
 */
public class HtmlRate extends UIInput {
	private int maxRates = 10;
	private boolean disabled = false;
	private String style;
	private String styleClass;
	private final String starRed = "/theme/default/images/common/star_red_s.gif";
	private final String starHalf = "/theme/default/images/common/star_half_s.gif";

	private final String activeImageSrc = "/theme/default/images/common/starActive.gif";
	private final String inactiveImageSrc = "/theme/default/images/common/starInactive.gif";

	@Override
	public String getRendererType() {
		return null;
	}

	@Override
	public final void encodeBegin(final FacesContext facesCtx)
			throws IOException {
		final ResponseWriter rw = facesCtx.getResponseWriter();

		float value = 0;
		if (getValue() != null) {
			value = ((Number) getValue()).floatValue();
		}

		final String spanId = facesCtx.getViewRoot().createUniqueId();
		final String gavenRateVar = getClientId(facesCtx) + "_targetRate";

		rw.startElement("div", this);
		if (style != null) {
			rw.writeAttribute("style", style, null);
		}
		if (styleClass != null) {
			rw.writeAttribute("class", styleClass, null);
		}
		rw.writeAttribute("id", spanId, null);
		rw.startElement("input", this);
		rw.writeAttribute("type", "hidden", null);
		rw.writeAttribute("id", gavenRateVar, null);
		rw.writeAttribute("name", gavenRateVar, null);
		rw.writeAttribute("value", value, null);
		rw.endElement("input");

		if (!disabled) {
			for (int i = 1; i <= maxRates; i++) {
				final String formId = getParentFormId(this, facesCtx);
				rw.startElement("a", this);
				rw.writeAttribute("href", "#", null);
				rw.writeAttribute("onClick", "document.forms['" + formId
						+ "']['" + gavenRateVar + "'].value = " + i, null);

				rw.startElement("img", this);
				rw.writeAttribute("style", "border:none", null);
				rw.writeAttribute("id", spanId + "_" + i, null);

				if (i <= value) {
					rw.writeAttribute("src", facesCtx.getExternalContext()
							.getRequestContextPath()
							+ activeImageSrc, null);
				} else {
					rw.writeAttribute("src", facesCtx.getExternalContext()
							.getRequestContextPath()
							+ inactiveImageSrc, null);
				}

				rw.writeAttribute("onMouseOver",
						"return pripremiZaOcjenu(this);", null);
				rw.writeAttribute("onMouseOut",
						"return vratiUPrethodnoStanje(this,'" + gavenRateVar
								+ "');", null);
				rw.endElement("img");
				rw.endElement("a");
			}
		} else {
			for (int i = 1; i <= value; i++) {
				rw.startElement("img", this);
				rw.writeAttribute("style", "border:none", null);
				rw.writeAttribute("id", spanId + "_" + i, null);

				if (i <= value) {
					rw.writeAttribute("src", facesCtx.getExternalContext()
							.getRequestContextPath()
							+ starRed, null);
				} else {
					if (value > Math.floor(value)) {
						if (value >= Math.floor(value) + 0.5) {
							rw.writeAttribute("src", facesCtx
									.getExternalContext()
									.getRequestContextPath()
									+ starHalf, null);
						}
					}
				}

				rw.endElement("img");
			}
		}
		rw.flush();
		rw.close();
	}

	@Override
	public final void encodeEnd(final FacesContext facesCtx) throws IOException {
		super.encodeEnd(facesCtx);
		final ResponseWriter rw = facesCtx.getResponseWriter();
		rw.endElement("div");
		rw.flush();
		rw.close();
	}

	private String getParentFormId(final UIComponent component,
			final FacesContext ctx) {
		if (component.getParent() instanceof HtmlForm) {
			return component.getParent().getClientId(ctx);
		} else {
			return getParentFormId(component.getParent(), ctx);
		}
	}

	@Override
	public final void decode(final FacesContext context) {
		final Map<String, String> requestMap = context.getExternalContext()
				.getRequestParameterMap();
		if (!disabled) {
			final String rate = requestMap.get(getClientId(context)
					+ "_targetRate");
			if (rate != null && !rate.equals("")) {
				try {
					setValue(Short.parseShort(rate));
				} catch (final NumberFormatException e) {
					setValue(Float.valueOf(rate).shortValue());
				}
			}
			getValueExpression("value").setValue(context.getELContext(),
					getValue());
			// queueEvent(new ActionEvent(this));
		}
	}

	@Override
	public final Object saveState(final FacesContext context) {
		final Object[] values = new Object[2];
		values[0] = super.saveState(context);
		values[1] = disabled;
		return values;
	}

	@Override
	public final void restoreState(final FacesContext context,
			final Object state) {
		final Object[] values = (Object[]) state;
		super.restoreState(context, values[0]);
		disabled = (Boolean) values[1];
	}

	public final int getMaxRates() {
		return maxRates;
	}

	public final void setMaxRates(final int maxRates) {
		this.maxRates = maxRates;
	}

	public final boolean isDisabled() {
		return disabled;
	}

	public final void setDisabled(final boolean disabled) {
		this.disabled = disabled;
	}

	public final String getStyle() {
		return style;
	}

	public final void setStyle(final String style) {
		this.style = style;
	}

	public final String getStyleClass() {
		return styleClass;
	}

	public final void setStyleClass(final String styleClass) {
		this.styleClass = styleClass;
	}
}
